﻿
namespace FBProject.BusinessLogic.MSProjectProxies
{
    public class MSProjectApplicationProxy
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(MSProjectApplicationProxy));

        #region Static Members

        public static bool IsMSProjectInstalled
        {
            get
            {
                try
                {
                    Logger.Debug("IsMSProjectInstalled - Creating Microsoft.Office.Interop.MSProject.Application");
                    var test = new Microsoft.Office.Interop.MSProject.Application();
                    Logger.Debug("IsMSProjectInstalled - Microsoft.Office.Interop.MSProject.Application succesfully created");
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    Logger.Warn("IsMSProjectInstalled - Caught COMException", ex);
                    return false;
                }
                return true;
            }
        }

        private static Microsoft.Office.Interop.MSProject.Application _MSProjectApplication = null;
        public static Microsoft.Office.Interop.MSProject.Application GetMSProjectApplication()
        {
            if (_MSProjectApplication == null)
            {
                try
                {
                    Logger.Info("Creating Microsoft.Office.Interop.MSProject.Application");
                    _MSProjectApplication = new Microsoft.Office.Interop.MSProject.Application();
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    Logger.Error("MSProjectApplication - Exception", ex);
                    throw new CannotAccessMSProjectException(ex);
                }
            }
            return _MSProjectApplication;
        }

        public static Microsoft.Office.Interop.MSProject._IProjectDoc GetActiveProject()
        {
            try
            {
                var activeProject = GetMSProjectApplication().ActiveProject;
                if (activeProject == null)
                    throw new NoActiveMSProjectException();

                return activeProject;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Logger.Error("ActiveProject - Exception", ex);
                RefreshMSProjectApplication();
                throw new CannotAccessMSProjectException(ex);
            }
        }

        private static void RefreshMSProjectApplication()
        {
            _MSProjectApplication = null;
        }

        public static bool CreateNewMSProject()
        {
            if (GetMSProjectApplication().FileNew())
            {
                DeleteColumn("Duration");
                AddColumn(1, "Text1", "Case#", 8);
                AddColumn(3, "Number1", "Estimate", 8);
                AddColumn(4, "Remaining Work", "", 8);
                AddColumn(9, "Text2", "Milestone", 10);
                AddColumn(10, "Priority", "", 5);
                AddColumn(11, "Flag1", "Ignore", 5);

                GetMSProjectApplication().SelectTaskField(Row: 1, Column: "Indicators", RowRelative: false);

                GetMSProjectApplication().Visible = true;

                return true;
            }
            return false;
        }

        public static bool OpenMSProject()
        {
            GetMSProjectApplication().Visible = true;
            return GetMSProjectApplication().FileOpenEx();
        }

        internal static bool OpenMSProject(string name)
        {
            GetMSProjectApplication().Visible = true;
            return GetMSProjectApplication().FileOpenEx(Name: name);
        }

        private static void AddColumn(int columnPosition, string fieldName, string title, int width)
        {
            GetMSProjectApplication().TableEditEx(Name: "&Entry", ColumnPosition: columnPosition, NewFieldName: fieldName, Title: title, Width: width, NewName: "", TaskTable: true, ShowInMenu: true, LockFirstColumn: true, DateFormat: 255, RowHeight: 1);
            GetMSProjectApplication().TableApply(Name: "&Entry");
        }

        private static void DeleteColumn(string fieldName)
        {
            GetMSProjectApplication().SelectTaskColumn(Column: fieldName);
            GetMSProjectApplication().ColumnDelete();
        }

        #endregion
    }
}
